/* eslint-disable array-bracket-spacing */
/* eslint-disable no-dupe-class-members */
/* eslint-disable indent */
'use strict'

const Service = require('egg').Service
const dayjs = require('dayjs')
const { Op } = require('sequelize')

// 信息要返回的字段
const attributes = ['id', 'm_bid', 'pid', 'games']
const member_attributes = ['id', 'mid', 'openid', 'unionid', 'phone', 'nickname', 'avatar', 'sex', 'birthday', 'register_time', 'last_login_time', 'level_name', 'subscribe', 'subscribe_time', 'pid', 'card_id', 'balance', 'recharge_amount', 'consume_time', 'week_count', 'count', 'status']
const place_attributes = ['barname', 'barcode']

class GameInfoService extends Service {

    // 查询所有列表
    async getGameInfoList(params) {
        const { model, helper } = this.ctx
        const currentPage = params?.pageIndex ?? 1
        const pageSize = params?.pageSize ?? 200

        const where = {}
        if (params.pid !== undefined && params.pid != null) {
            where.pid = params.pid
        }
        return await model.OnlineMember.findAndCountAll({
            where,
            offset: (currentPage - 1) * pageSize, // 计算分页偏移量
            limit: pageSize,
            attributes,
            include: [
                {
                    model: model.Place,
                    // as: 'place',
                    attributes: place_attributes
                },
                {
                    model: model.Member,
                    // as: 'member',
                    attributes: member_attributes
                }
            ],
            order: [
                ['computer_name', 'ASC'],
                ['upmachine_time', 'DESC']
            ]
        })
            .then(async result => {
                if (result) {
                    return {
                        list: result.rows,
                        total: result.count
                    }
                }
                helper.failed(1008, null, '查询会员列表失败')
                return false
            })
            .catch(async error => {
                helper.failed(1001, null, error?.errors[0]?.message)
                return false
            })
    }

    // 更新游戏信息
    async editGamesInfo(params) {
        const { model, helper, logger } = this.ctx

        // 先查询门店信息
        const place = await model.Member.findOne({
            where: {
                pid: params.barcode
            }
        })
        if (!place) {
            helper.failed(1004, null, '门店信息不存在')
            return false
        }

        // 先查询会员信息是否存在
        const member = await model.Member.findOne({
            where: {
                mid: params.mid,
                pid: place.id
            }
        })
        if (!member) {
            helper.failed(1004, null, '会员信息不存在')
            return false
        }
        // 查询指定PID和MID会员的游戏信息
        return await model.GameInfo.findOne({
            pid: params.pid,
            m_bid: member.id
        })
            .then(async result => {
                if (result) {
                    return await result.update({
                        client_name: params.client_name,
                        games: params.games
                    })
                        .then(async data => {
                            if (data) {
                                return data
                            }
                            helper.failed(1006, null, '更新游戏信息失败')
                            return false
                        })
                        .catch(async error => {
                            if (error?.original?.sqlMessage) {
                                logger.error(`更新游戏信息失败   barcode:[${params.barcode}]   ${error?.original?.sqlMessage}`)
                                helper.failed(1001, null, error?.original?.sqlMessage)
                            } else if (error?.errors[0]?.message) {
                                logger.error(`更新游戏信息失败   barcode:[${params.barcode}]    ${error.errors[0]?.message}`)
                                helper.failed(1001, null, error.errors[0]?.message)
                            } else {
                                logger.error(`更新游戏信息失败   barcode:[${params.barcode}]   ${JSON.stringify(error)}`)
                                helper.failed(1001, null, JSON.stringify(error))
                            }
                            return false
                        })
                }
                helper.failed(1004, null, '门店不存在')
                return false
            })
            .catch(async error => {
                if (error?.original?.sqlMessage) {
                    logger.error(`更新游戏信息查询失败   barcode:[${params.barcode}]   ${error?.original?.sqlMessage}`)
                    helper.failed(1001, null, error?.original?.sqlMessage)
                } else if (error?.errors[0]?.message) {
                    logger.error(`更新游戏信息查询失败   barcode:[${params.barcode}]    ${error.errors[0]?.message}`)
                    helper.failed(1001, null, error.errors[0]?.message)
                } else {
                    logger.error(`更新游戏信息查询失败   barcode:[${params.barcode}]   ${JSON.stringify(error)}`)
                    helper.failed(1001, null, JSON.stringify(error))
                }
                return false
            })
    }
}

module.exports = GameInfoService
